package com.shenzhen.horselive.presenter;


import io.reactivex.disposables.Disposable;

/**
 * 1、view如果为空，自动取消订阅
 * 2、view处于活跃状态，回调{@link #onNextInActive(Object)}
 *
 * @param <T>
 */
public abstract class ViewObserver<T> implements io.reactivex.Observer<T> {


    private BaseView view;
    private Disposable d;


    public ViewObserver(BaseView view) {
        this.view = view;

    }


    @Override
    public void onNext(T t) {
        if (view.isActive()) {
            onNextInActive(t);
        }
    }

    protected abstract void onNextInActive(T t);

    @Override
    public void onSubscribe(Disposable d) {
        this.d = d;
        if (view == null) {
            d.dispose();
            return;
        }
    }

    @Override
    public void onError(Throwable e) {
        e.printStackTrace();
        view.showError(e);

    }

    public void cancel() {
        if (d != null) {
            d.dispose();
        }
    }

    @Override
    public void onComplete() {

    }
}